package Utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

public class ReplaceInFile {
	private static HashMap<String,String> tokenS ;
	public static void replaceInFile(String filePath,HashMap<String,String> map) throws IOException{
		tokenS = map ;
		File outF = new File(filePath+System.currentTimeMillis());
		File inF = new File(filePath);
		BufferedReader reader = new BufferedReader(new FileReader(inF));
		BufferedWriter out = new BufferedWriter(new FileWriter(outF));
		String line ;
		while ((line=reader.readLine())!=null){
			out.write(replaceAll(line)+"\n");
		}
		out.flush();
		out.close();
		reader.close();
		inF.delete();
		outF.renameTo(new File(filePath));
	}

	public static void replaceInFile(String filePath,String outFile,HashMap<String,String> map) throws IOException{
		tokenS = map ;
		File outF = new File(outFile);
		File inF = new File(filePath);
		BufferedReader reader = new BufferedReader(new FileReader(inF));
		BufferedWriter out = new BufferedWriter(new FileWriter(outF));
		String line ;
		while ((line=reader.readLine())!=null){
			out.write(replaceAll(line)+"\n");
		}
		out.flush();
		out.close();
		reader.close();
	}
	
	public static String replaceAll(String s){
		for(String k : tokenS.keySet()){
			s=s.replaceAll(k, tokenS.get(k));
		}
		return s ;
	}

	public static void main (String [] a) throws IOException{
		HashMap<String, String> tokens = new HashMap<String, String>();
		tokens.put("\"DECL2\"","\"function_(_)_\"");
		tokens.put("\"EXP10\"","\"_>=_\"");
		tokens.put("\"EXP11\"","\"_==_\"");
		tokens.put("\"EXP3\"","\"_/_\"");
		tokens.put("\"STMT15\"","\"rendezvous_;\"");
		tokens.put("\"STMT16\"","\"print(_);\"");
		tokens.put("\"STMT14\"","\"release_;\"");
		tokens.put("\"EXP9\"","\"_>_\"");
		tokens.put("\"STMT9\"","\"write(_);\"");
		tokens.put("\"STMT2\"","\"_;\"");
		tokens.put("\"STMT10\"","\"try_catch(_)_\"");
		tokens.put("\"STMT12\"","\"spawn_\"");
		tokens.put("\"EXP15\"","\"_=_\"");
		tokens.put("\"EXP0\"","\"[_]\"");
		tokens.put("\"STMT13\"","\"acquire_;\"");
		tokens.put("\"STMT5\"","\"while_do_\"");
		tokens.put("\"STMT11\"","\"throw_;\"");
		tokens.put("\"EXP5\"","\"_+_\"");
		tokens.put("\"EXP_18\"","\"read(_)\"");
		tokens.put("\"STMT3\"","\"if_then_else_\"");
		tokens.put("\"EXP4\"","\"_%_\"");
		tokens.put("\"DECL0\"","\"var_;\"");
		tokens.put("\"EXP_15\"","\"++_\"");
		tokens.put("\"EXP13\"","\"_and_\"");
		tokens.put("\"EXP1\"","\"(_)\"");
		tokens.put("\"STMT6\"","\"for_=_to_do_\"");
		tokens.put("\"EXP8\"","\"_<=_\"");
		tokens.put("\"EXP_17\"","\"sizeOf(_)\"");
		tokens.put("\"EXP12\"","\"_!=_\"");
		tokens.put("\"STMT1\"","\"{_}\"");
		tokens.put("\"STMT8\"","\"return_;\"");
		tokens.put("\"EXP7\"","\"_<_\"");
		tokens.put("\"EXP_19\"","\"not_\"");
		tokens.put("\"STMT4\"","\"if_then_\"");
		tokens.put("\"EXP14\"","\"_or_\"");
		tokens.put("\"EXP2\"","\"_*_\"");
		tokens.put("\"EXP6\"","\"_-_\"");
		tokens.put("\"EXPS0\"","\"_,_\"");
		replaceInFile("G:\\workspace\\grammarast\\src\\simp3ASTParser.java",tokens);
	}

}
